package cz.vutbr.fit.xzemko01.tfmcbs.validation.validators;

import java.util.Map;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;

import cz.vutbr.fit.xzemko01.tfmcbs.model.Model;
import cz.vutbr.fit.xzemko01.tfmcbs.validation.utils.DiagnosticBuilder;

public final class ModelValidator implements Validator<Model> {
	
	private static ModelValidator modelValidator;
	
	private ModelValidator() {
	}
	
	public static Validator<Model> getInstance() {
		if(modelValidator == null) {
			modelValidator = new ModelValidator();
		}
		return modelValidator;
	}	
	
	@Override
	public boolean validate(Model model, DiagnosticChain diagnostics, Map<Object, Object> context) {
		boolean result = validateModelHasTitle(model, diagnostics, context);
		result &= validateModelHasAuthor(model, diagnostics, context);
		return result;
	}
	
	private boolean validateModelHasTitle(Model model, DiagnosticChain diagnostics, Map<Object, Object> context) {
		String title = model.getTitle();
		boolean result = true;
		
		if(title == null || title.trim().isEmpty()) {
			Diagnostic diagnostic = new DiagnosticBuilder(model)
											.setMessage("Model's title is empty.").build();
			diagnostics.add(diagnostic);
			result = false;
		}
		return result;
	}
	
	private boolean validateModelHasAuthor(Model model, DiagnosticChain diagnostics, Map<Object, Object> context) {
		String title = model.getAuthor();
		boolean result = true;
		
		if(title == null || title.trim().isEmpty()) {
			Diagnostic diagnostic = new DiagnosticBuilder(model)
				.setMessage("Model's author name is empty.")
				.setSeverity(Diagnostic.WARNING).build();
			diagnostics.add(diagnostic);
			result = false;
		}
		return result;
	}
}
